// 管理员部门管理内容组件
Vue.component('admin-department-content', {
  data() {
    return {
      departments: [],
      dialogVisible: false,
      editDepartment: { id: '', name: '' },
      isEdit: false,
      loading: false,
      // 分页功能
      currentPage: 1,
      pageSize: 10,
      totalRecords: 0
    };
  },
  async mounted() {
    await this.loadDepartments();
  },
  template: `
    <div style="padding: 20px;">
      <el-card shadow="hover">
        <div style="font-size: 22px; font-weight: bold; margin-bottom: 18px;">部门管理</div>
        <div style="margin-bottom: 15px; display: flex; justify-content: flex-end; align-items: center;">
          <el-button type="primary" size="small" @click="openAddDialog">添加部门</el-button>
        </div>
        <el-table :data="departments" style="width: 100%;" border stripe v-loading="loading">
          <el-table-column prop="id" label="ID" width="80"></el-table-column>
          <el-table-column prop="name" label="部门名称" width="150"></el-table-column>
          <el-table-column prop="employee_count" label="人数" width="100"></el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="openEditDialog(scope.row)">编辑</el-button>
              <el-button type="text" size="mini" @click="deleteDepartment(scope.row)" style="color: #F56C6C;">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <!-- 分页组件 -->
        <div style="margin: 18px 0 0; text-align: right;">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="totalRecords"
            :page-size="pageSize"
            :current-page.sync="currentPage"
            @current-change="handlePageChange"
          ></el-pagination>
        </div>
        
        <el-dialog :title="isEdit ? '编辑部门' : '添加部门'" :visible.sync="dialogVisible" width="400px">
          <el-form :model="editDepartment" label-width="80px">
            <el-form-item label="部门名称">
              <el-input v-model="editDepartment.name" placeholder="请输入部门名称"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveDepartment">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  `,
  methods: {
    // 加载部门数据
    async loadDepartments() {
      this.loading = true;
      try {
        const params = {
          page: this.currentPage,
          page_size: this.pageSize
        };
        
        const response = await departmentAPI.getDepartments(params);
        this.departments = response.data.departments || [];
        this.totalRecords = response.data.total || 0;
      } catch (error) {
        console.error('获取部门数据失败:', error);
        this.$message.error('获取部门数据失败');
      } finally {
        this.loading = false;
      }
    },
    
    handlePageChange(page) {
      this.currentPage = page;
      this.loadDepartments();
    },
    
    openAddDialog() {
      this.isEdit = false;
      this.editDepartment = { id: '', name: '' };
      this.dialogVisible = true;
    },
    
    openEditDialog(row) {
      this.isEdit = true;
      this.editDepartment = Object.assign({}, row);
      this.dialogVisible = true;
    },
    
    async saveDepartment() {
      if (!this.editDepartment.name) {
        this.$message.error('请输入部门名称');
        return;
      }
      
      try {
        if (this.isEdit) {
          await departmentAPI.updateDepartment(this.editDepartment.id, this.editDepartment);
          this.$message.success('部门信息已更新');
        } else {
          await departmentAPI.createDepartment(this.editDepartment);
          this.$message.success('部门已添加');
          this.currentPage = 1;
        }
        this.dialogVisible = false;
        this.loadDepartments();
      } catch (error) {
        console.error('保存部门失败:', error);
        this.$message.error(error.message || '保存部门失败');
      }
    },
    
    async deleteDepartment(row) {
      this.$confirm('确定要删除该部门吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          await departmentAPI.deleteDepartment(row.id);
          this.$message.success('部门已删除');
          
          // 删除后检查当前页是否还有数据
          const totalPages = Math.ceil((this.totalRecords - 1) / this.pageSize);
          if (this.currentPage > totalPages && totalPages > 0) {
            this.currentPage = totalPages;
          }
          
          this.loadDepartments();
        } catch (error) {
          console.error('删除部门失败:', error);
          this.$message.error(error.message || '删除部门失败');
        }
      });
    }
  }
});